                      AUTO-COPYING

         M C Evans with a routine to enable you
          to run ZX-81 programs on a Spectrum.

This program, written for the 48K Spectrum, will load ZX-81
program tapes and transform them so they are able to run on
the Spectrum.
  After the Basic program has been typed in from the
program listing, which should not present any problem, run
from line 9900 which will Save and Verify the program on
tape.
  When the Basic program has been Saved run from line 10.
[The program on the TZX auto-runs from here, of course.]
First the c() array will be filled with the Spectrum codes
in the ZX-81 position, and the machine-code programs will
be Poked into the correct locations. All this information
is held in data statements from line 9000 onwards. At the
end of each data line there is a check-sum digit for the
line. If an error occurs, then the offending line will be
listed ready for editing. After the line is corrected,
restore the data and Run the program again.
  Once the data has been loaded correctly you will be asked
to insert the ZX-81 program tape and press enter. The first
time you try to run this program I would suggest that you
use a tape that you have purchased, since the quality of
the recording is probably better than you are able to
achieve. Start the tape as close as possible to the begin-
ning of the program - this is to avoid the loading of lots
of noise from a poor recording.
  If the tape is noisy the solid border colours will
change. Generally immediately prior to the start of the
program there is a silent area; if necessary stop the tape
recorder at this point and press Break on the Spectrum. To
restart the program type Goto 500 then restart the tape
recorder. I find that to load normal ZX-81 and Spectrum
tapes that the volume on the tape recorder is set to about
75 percent. To load ZX-81 tapes on the Spectrum I have to
use about 80 percent volume for tapes that I have purchased
and about 95 percent volume for my own recordings.
  The correct volume settings will have to be found by
trial and error. If you listen to the noise made by the
Spectrum when loading programs this will give a good idea
of the volume required.
  When the end of the ZX-81 program has been reached the
border colours will revert to the solid changing colours.
At this point press the Space-key - not Shift space or
Break. The Basic program will then continue by asking for
the name of the ZX-81 program that you have just loaded.
The Basic program will then proceed to try and find the
name among the information it has read from the tape.
  When the ZX-81 program is being loaded, all the informa-
tion that comes from the tape recorder is stored as it is
received. This may mean, if the tape was noisy prior to the
start of the program, that the program name is not at its
correct position. The Basic program therefore looks at the
first few storage locations to see if it can find the name.
  If it cannot, it will then shift each bit along one
position and repeat the search up to eight times for the
program name. If the program name is not found, change the
tape recorder settings and start the program again from
line 500. If you still fail to find the program name then
it will be necessary to re-record the ZX-81 program, taking
great care to produce the best quality recording you can.
If you entered the wrong program name, then restart the
Basic program from line 1000 and enter the correct name.
  Alternatively it may be possible to transfer the program
directly from the ZX-81 to the Spectrum using a tape
recorder and amplifier from an audio system. To do this,
first load the program into the ZX-81. Then connect the Mic
lead from the ZX-81 to the microphone input of the tape
recorder - if it is stereo, remember which channel you are
using.
  Connect the headphone output of the amplifier to the Ear
lead of the Spectrum; if it is a stereo amplifier turn the
balance control so that only the correct channel is being
used. Put a tape in the recorder and start recording; this
will link the tape recorder and amplifier. Start the Basic
program in the Spectrum from line 500 and finally instruct
the ZX-81 to save its program.
  This causes the ZX-81 program to simultaneously recorded
on tape, which can then be discarded, and play the recor-
ding signal directly to the Spectrum, which thinks it is
reading a tape. I found that the best results were obtained
when as few as possible modifications were made to the
recording signal, i.e. no Dolby, peak noise filter, bass,
treble and other sound enhancements. Despite the number of
leads and power supplied required, I found this method to
be the most trouble-free. It also works Spectrum to
Spectrum.
  Having found the name of the ZX-81 program, the rest of
the program is then converted so that it is able to run on
the Spectrum with the following differences to statements.
  Slow, Fast, Poke and Usr are replaced with Rem to stop
the converted program accidentally crashing. Unplot is
replaced with Plot and the shaded ZX-81 graphic characters
are replaced with the Spectrum's user definable graphics -
which you will have to define later.
  If a legitimate Rem statement is encountered, you are
asked if the line contains machine code and, if so, if you
want to Save the machine code on tape.
  After the ZX-81 program has been converted you are
prompted for a program name so the converted program can be
Saved on tape. The Saved program can then be loaded back
into the Spectrum as a normal Spectrum program. Most pro-
grams that are free of Peek, Poke and Usr statements are
then able to run without any further modifications.
  If having found the name an error occurs during the con-
version, you will be prompted to Save the program, so far
converted, on tape. After the program has been Saved an
attempt is made to find a new entry point into the ZX-81
program. Thus several sections of the ZX-81 programs can be
Saved on tape and later using the Spectrum's Merge command,
the programs combined together into one program, plus a few
gaps. If errors do occur it is indicative of a wrong volume
setting or a poor recording.
  There are also four machine-code programs used by the
Basic program during the course of loading and converting
the ZX-81 program. The first machine-code program inter-
prets the ZX-81 tape signals and stores each bit of infor-
mation as it is received. The signal on a ZX-81 tape has
the following form:

On         __    __    __    __    _   ______         0 bit
Off     __|  |__|  |__|  |__|  |__| |_|      |______ 

On    __    __    __    __    __    __    __    __    __    _   ______  1 bit
Off__|  |__|  |__|  |__|  |__|  |__|  |__|  |__|  |__|  |__| |_|      |______

  There are four or nine regular on and off signals - indi-
cating 0 bit or 1 bit respectively - followed by a short on
and off followed by a long on and off - which indicates the
end of the bit. In the ZX-81 these signals are decoded by
counting the time between reaching the start of the long
off signal, from the previous bit, and deciding that the
long on has been reached. The distribution of time taken to
decode a bit looks like this:

   |        :          :                :
   | Error  :   0 bit  :  1 bit         : Error
   |        :     _    :    _           :
   |        :    | |   :   | |          :
   |        :    | |   :   | |          :
   |        :    | |   :   | |          :
   |________:____|_|___:___|_|__________:________
  94  80   56          0               80
           Hex value in E register

  The E register is used as a counter and starts with the
value Hex 94, decreasing by one each time it passes through
the loop. A long on signal is deemed to have been reached
when the signal has been on for 26 loops.
  If the long on is reached prior to the counter reaching
Hex 56 then an error is assumed to have occurred and the
counter starts again from Hex 94. If an error occurs any-
where else but the first bit, then the program will not
load correctly. If a valid long on occurs before the coun-
ter reaches Hex 00 then the bit is an 0, otherwise the bit
is 1. The grouping of time for the 0's and 1's is very
compact and the ZX-81 seems difficult at interpreting the 
signal.
  If the same procedure is followed on the Spectum the
following distribution occurs:
           
               _       |       _
              | |      |      | |
              | |      |      | |
             |   |     |     |   |
             |   |     |     |   |
           _/     \_   |   _/     \_
    ______/_________\__|__/_________\______
                       |

  Instead of the grouping being compact, they appear like
the statistical normal distribution with the accompanying
tails. To get the distribution into the same form, the
setting of the volume and tone controls on the tape recor-
der are critical - which is not the case on the ZX-81 -
otherwise the distribution looks rectangular.
  The next problem is that different tape recorder settings
cause the peaks, and more importantly the tails, of the
distribution to move. This then leads to the tail crossing
the boundary between a 0 and 1 bit.
  These differences between the ZX-81 and the Spectrum mean
that it is not possible to use the ZX-81 method of decoding
the tape signal and this is why greater care has to be
taken when recording the ZX-81 programs to avoid the square
shape of the signal becoming unduly rounded thus making it
difficult to pick out the long on signal correctly.
  The problem of knowing when the end of a bit has been
reached is overcome by going back to first principles: by
counting the on signals and looking at the long on signal
to see if it occurs roughly on the fifth or tenth signal.
The loading routine is the first of the machine-code pro-
grams and is very straightforward and simply reads the tape
and stores each bit as it comes from the tape for inter-
pretation later.
  This leads to the problem that if there is any noise on
the tape prior to the start of the program then the pro-
gram itself will not be at the start location and each bit
of data may not be at its correct position in the byte.
  After a program has been loaded into the memory the next
problem is to get each bit into correct position within the
byte. This is done by the second machine code program try-
ing to find the name of the ZX-81 program somewhere within
the first few storage locations. To do this the Basic pro-
gram first asks for the name of the program already loaded,
then looks at about the first twenty locations to see if
the name occurs. If it does not, then each bit is moved one
place, as shown below, until the name is found.

   11111100-+     01111110-+     00111111
            |              |            Z
 +-----<----+   +-----<----+
 |              |
 +-11111100-+   +-01111010-+     00111101
            |              |            X
 +-----<----+   +-----<----+
 |              |
 +-10010000-+   +-01001000-+     00100100
            |              |            8
 +-----<----+   +-----<----+
 |              |
 +-01110100     +-00111010       00011101
          -------->      -------->      1

The third machine-code program is used to find a new entry
point into the ZX-81 program by looking through the 16K
storage area to see if it can find a code 118 (NEWLINE).

[   To my astonishment and delight, this program actually
  still works on at least one emulator. It does not work
  on all emulators. ZX Spin, at least the version I use,
  does not know how to read ZX81 tapes, even if they're
  saved as TZXes. EightyOne, however, handles it as in-
  tended, and the resulting Spectrum tape will load fine
  in other emulators. The obvious reason for this diffe-
  rence is that ZX Spin is a dedicated Spectrum emulator,
  whereas EightyOne emulates both the Spectrum and the
  ZX81 (amongst others). Keep this in mind when trying
  this program on other emulators - it is most likely to
  work if your emulator already handles the ZX81.
    The TZX which goes with this text file contains both
  the program itself, and a mergeable snippet which loads
  the first six UDGs with the ZX81's shaded graphics. It
  should also be accompanied by "Centipede.p", a ZX81
  tape file holding a small game with which to try the
  program. It uses some shaded graphics, and presents one
  or two conversion hiccups, but does not use any of the
  statements which the program cannot handle, nor does it
  peek the screen memory or depend on the ZX81's charset
  codes. It does, therefore, work without any more complex
  after-conversion massaging. Merge it with "shades";
  only a few small adaptations will be necessary. If you
  want to get a perfect conversion, note that lower-case
  letters were inverse video on the ZX81.
                                Richard Bos, Januari 2012 ]